package ru.tennisworld.match.services.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import ru.tennisworld.match.data.enums.MatchStageEnum;
import ru.tennisworld.match.entities.base.BaseReferenceEntity;
import ru.tennisworld.match.entities.references.MatchStage;
import ru.tennisworld.match.repositories.MatchStageRepository;
import ru.tennisworld.match.repositories.base.BaseReferenceEntityRepository;
import ru.tennisworld.match.services.ReferenceService;

@Service("referenceService")
public class ReferenceServiceImpl implements ReferenceService {

    @Autowired
    private MatchStageRepository matchStageRepository;

    @Override
    public MatchStage getMatchStageByCode(String code) {
        return getReferenceByCode(matchStageRepository, code);
    }

    @Override
    public MatchStage getMatchStage(MatchStageEnum matchStage) {
        return matchStage == null ? null : getReferenceByCode(matchStageRepository, matchStage.getCode());
    }

    private <T extends BaseReferenceEntity> T getReference(BaseReferenceEntityRepository<T> repository, Long id) {
        return repository.getReferenceById(id);
    }

    private <T extends BaseReferenceEntity> T getReferenceByCode(BaseReferenceEntityRepository<T> repository, String code) {
        return repository.getReferenceByCode(code);
    }
}
